//
//  DEWApplyForView.m
//  OtherDemo
//
//  Created by kangbinbin on 2018/7/29.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWApplyForView.h"
#import "IQTextView.h"
#import "MBProgressHUD+Add.h"
@interface DEWApplyForView ()
/** <#type#> */
@property (strong, nonatomic) UIView *AlertView;
/** <#type#> */
@property (strong, nonatomic) IQTextView *textView;


@end
@implementation DEWApplyForView

+ (DEWApplyForView *)showApplyForViewpushbtnBlock:(pushbtnBlock)pushbtnBlock;{
    DEWApplyForView *Pop = [[DEWApplyForView alloc]initWithDataArrPushbtnBlock:pushbtnBlock];
    return Pop;
    
}
-(instancetype)initWithDataArrPushbtnBlock:(pushbtnBlock)pushbtnBlock{
    if (self = [super init]) {
        _pushbtnBlock = [pushbtnBlock copy];
        [self creatView];
    }
    return self;
}

- (void)creatView{
    [self initUI];
    
    [self show];
    
    
}
-(void)initUI{
    
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    self.frame = CGRectMake(0, 0, DEWScreenWidth, DEWScreenHeight);
    
    UIView *AlertView = [[UIView alloc]initWithFrame:CGRectMake(43, 219, DEWScreenWidth - 43 * 2 ,190)];
    AlertView.backgroundColor = [UIColor whiteColor];
    AlertView.layer.cornerRadius = 10;
    [self addSubview:AlertView];
    self.AlertView = AlertView;
    
    UILabel *titleL = [[UILabel alloc]init];
    titleL.frame = CGRectMake(0, 22, AlertView.width, 30);
    titleL.text = @"申请理由";
    titleL.textColor = UIColorFromRGB(0x333333);
    titleL.font = [UIFont systemFontOfSize:19.f];
    titleL.textAlignment = NSTextAlignmentCenter;
    [AlertView addSubview:titleL];
    
    
    IQTextView *textView = [[IQTextView alloc]initWithFrame:CGRectMake(25, titleL.Wbotoom + 17, AlertView.width - 50, 60)];
    textView.placeholder = @"请填写你的申请理由";
    textView.font = [UIFont systemFontOfSize:13];
    textView.layer.borderColor = Color_99.CGColor;
    textView.layer.borderWidth = 1;
    textView.layer.cornerRadius = 3;
    [AlertView addSubview:textView];
    self.textView = textView;
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, textView.Wbotoom + 19, AlertView.width , 1)];
    lineView.backgroundColor = UIColorFromRGB(0xDCDCDC);
    [AlertView addSubview:lineView];
    
    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(AlertView.width / 2, lineView.y, 1 , 44)];
    lineView1.backgroundColor = UIColorFromRGB(0xDCDCDC);
    [AlertView addSubview:lineView1];
    
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, lineView.bottom, AlertView.width / 2, 45);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:Color_99 forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.tag = 51;
    [AlertView addSubview:cancelBtn];
    
    
    UIButton *quedingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [AlertView addSubview:quedingBtn];
    quedingBtn.frame = CGRectMake(AlertView.width / 2,lineView.bottom, AlertView.width /2, 45);
    [quedingBtn setTitle:@"确定" forState:UIControlStateNormal];
    [quedingBtn setTitleColor:UIColorFromRGB(0xFF8900) forState:UIControlStateNormal];
    [quedingBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    quedingBtn.titleLabel.font = [UIFont systemFontOfSize:18];
//    quedingBtn.layer.cornerRadius = 4;
    quedingBtn.tag = 50;
    
    
}

-(void)buttonClick:(UIButton *)sender{
    if (sender.tag == 50) {//确定
        if ([_textView.text isEqualToString:@""]) {
            [MBProgressHUD showError:@"请填写你的理由" toView:[UIApplication sharedApplication].keyWindow];
            return;
        }
        if (self.pushbtnBlock) {
            self.pushbtnBlock(self.textView.text);
        }
        
    }else{//取消
        [self closeViewTime:0.5];
    }
    
}


-(void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.AlertView.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.AlertView.alpha = 1;
    }];
    
}
-(void)closeViewTime:(NSTimeInterval)duration{
    [UIView animateWithDuration:duration animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self];
    if (!CGRectContainsPoint([self.AlertView frame], pt)) {
        [self closeViewTime:0.5];
    }
    
}
@end
